package com.jdkj.tools.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.SneakyThrows;
import org.apache.http.Header;
import org.apache.http.message.BasicHeader;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class CreateDirectory {


    @SneakyThrows
    public static Integer init(String projectKey,String folderName) {
        Header [] headers = { new BasicHeader("cookie", TokenConstant.JSESSIONID)
        ,new BasicHeader("content-type","application/json")};
//        String getParentId="\n" +
//                "http://192.168.11.7:8083/rest/raven/1.0/folderStructure/folders?entityKey=EP202407&folderId=-1&_=1747289631123";
        StringBuffer getParentId=new StringBuffer("http://192.168.11.7:8083/rest/raven/1.0/folderStructure/folders?")
                .append("entityKey=").append(projectKey).append("&_=").append(System.currentTimeMillis()).append("&folderId=-1");
        System.out.println(getParentId.toString());
        String str = HttpClientUtil.doGet(getParentId.toString(), new HashMap<>(), headers);
        System.out.println("获取二级文件夹"+str);
        JSONObject jsonObject=  (JSONObject)JSONObject.parse(str);
        JSONObject folders = (JSONObject) jsonObject.getJSONArray("folders").get(0);
        String folderId = String.valueOf(folders.getInteger("id"));

        StringBuffer baseUrl=new StringBuffer("http://192.168.11.7:8083/rest/raven/1.0/folderStructure/create?")
                .append("name=").append(URLEncoder.encode(folderName, StandardCharsets.UTF_8))
                .append("&entityKey=").append(projectKey).append("&parentId=").append(folderId);

       // http://192.168.11.7:8083/rest/raven/1.0/folderStructure/create?name=%E6%96%B0%E6%96%87%E4%BB%B6%E5%A4%B9&entityKey=EP202401&parentId=4
//        System.out.println(baseUrl.toString());
        String res = HttpClientUtil.doPostObject(baseUrl.toString(), new HashMap<>(), headers);
        System.out.println("res"+res);
        JSONObject parse = (JSONObject) JSONObject.parse(res);
         return parse.getInteger("id");
    }

    public static void main(String[] args) {
        System.out.println(init("EP202401", "21234"));
    }
}
